from datetime import datetime

import pytest

from src.application.user.dto import UserDTO
from src.application.user.exceptions import UserIdNotExist
from src.application.user.use_cases import GetUserById, GetUserByIdHandler
from tests.mocks.uow import UnitOfWorkMock
from tests.mocks.user_reader import UserReaderMock


@pytest.mark.asyncio
async def test_get_user_correct(uow: UnitOfWorkMock, user_reader: UserReaderMock) -> None:
    user = UserDTO(id=0, tg_id=1413248651, username="web2py", was_registered=datetime.now())
    await user_reader.add_user(user)
    print(user_reader.users)

    command = GetUserById(0)
    handler = GetUserByIdHandler(uow=uow)

    get_user = await handler(command=command)

    assert get_user == user
    assert uow.commit_status is False
    assert uow.rollback_status is False


async def test_get_not_exist_user(uow: UnitOfWorkMock) -> None:
    command = GetUserById(user_id=0)
    handler = GetUserByIdHandler(uow=uow)

    with pytest.raises(UserIdNotExist):
        await handler(command=command)

    assert uow.commit_status is False
    assert uow.rollback_status is False
